package com.edoomath.mainapp.tencent.service.impl;

import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.wechat.entity.AuthScope;
import com.edoomath.entity.tecent.WebAuth;
import com.edoomath.entity.tecent.WebAuthClient;
import com.edoomath.entity.tecent.WebAuthConfig;
import com.edoomath.entity.tecent.WechatAccount;
import com.edoomath.entity.tecent.WechatOpenid;
import com.edoomath.entity.user.UserSource;
import com.edoomath.mainapp.tencent.dao.WebAuthDao;
import com.edoomath.mainapp.tencent.dao.WechatAccountDao;
import com.edoomath.mainapp.tencent.service.WebAuthService;

/**
 * 
 * @author mohanlan
 *
 */
@Service("webAuthService")
public class WebAuthServiceImpl implements WebAuthService {
    
    @Dao
    private WechatAccountDao wechatAccountDao;
    
    @Dao
    private WebAuthDao webAuthDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see com.demo2do.wechatmcs.service.WebAuthService#getWebAuthClient(java.lang.String)
     */
    public WebAuthClient getWebAuthClient(String host) {
        if(StringUtils.isNotBlank(host)) {
            return webAuthDao.loadWebAuthClient(host);
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.demo2do.wechatmcs.service.WebAuthService#createWebAuth(com.demo2do.wechatmcs.entity.webauth.WebAuth, com.demo2do.wechatmcs.entity.webauth.WebAuthConfig)
     */
    public void createWebAuth(WebAuth webAuth, WebAuthConfig config) {
        String uuid = UUID.randomUUID().toString();
        genericDaoSupport.save(webAuth.init(uuid, config));
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.wechatmcs.service.WebAuthService#getWebAuthRequest(java.lang.String)
     */
    public WebAuth getWebAuth(String state) {
        return webAuthDao.getWebAuth(state);
    }

    /* (non-Javadoc)
     * @see com.demo2do.wechatmcs.service.WebAuthService#loadWebAuth(java.lang.String)
     */
    public WebAuth loadWebAuth(String uuid) {
        
        // 1. load web_auth_request according to uuid
        WebAuth webAuth = genericDaoSupport.load(WebAuth.class, uuid);

        // 2. load wechat_account if auth_scope is USERINFO
        if(webAuth.matches(AuthScope.USERINFO)) {
            
            WechatAccount wechatAccount = wechatAccountDao.getWechatAccount(webAuth.getOpenid());
            
            // init wechat account
            webAuth.account(wechatAccount);
        }
        
        return webAuth;
    }

    /* (non-Javadoc)
     * @see com.demo2do.wechatmcs.service.WebAuthService#authorize(com.demo2do.wechatmcs.entity.webauth.WebAuth, java.lang.String)
     */
    public void authorize(WebAuth webAuth, String openid) {
        genericDaoSupport.update(webAuth.authorize(openid));
    }

    /* (non-Javadoc)
     * @see com.demo2do.wechatmcs.service.WebAuthService#refresh(com.demo2do.wechatmcs.entity.webauth.WebAuth, com.demo2do.wechatmcs.entity.webauth.WechatAccount)
     */
    public void refresh(WebAuth webAuth, WechatAccount wechatAccount) {
        
        String unionid = wechatAccount.getUnionid();
        
        // 1. load wechat account 
        WechatAccount account = wechatAccountDao.getWechatAccountByUnionid(unionid);
        
        // 2. refresh wechat account
        if(account == null) { 

            // 2.1 refresh back web_auth with account_id and unionid
            genericDaoSupport.save(wechatAccount);
            genericDaoSupport.update(webAuth.union(wechatAccount.getUnionid(), wechatAccount));
            
        } else {
            
            // 2.2 refresh account use new wechat account 
            genericDaoSupport.update(account.refresh(wechatAccount));
        }
        
        // 3. save wechat opengid here
        WechatOpenid wechatOpenid = wechatAccountDao.getWechatOpenid(unionid, UserSource.EDOOMATH_WECHAT_ACCOUNT);
        if(wechatOpenid == null) {
        	wechatOpenid = new WechatOpenid(wechatAccount.getOpenid(), unionid, UserSource.EDOOMATH_WECHAT_ACCOUNT);
        	genericDaoSupport.save(wechatAccount);
        }
    }

}
